package com.bnzj.cloud.business.core.service;

import com.bnzj.cloud.business.core.persistence.entity.SysRoleResource;
import com.bnzj.cloud.business.core.persistence.repository.SysResourceRoleRepository;
import com.bnzj.core.persistence.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysResourceRoleService extends BaseService<SysResourceRoleRepository, SysRoleResource, Long> {

    public List<SysRoleResource> findResourceRoleBy(Long id) {

        return repository.findResourceRoleBy(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean addOrEditJuri(List<SysRoleResource> resourceRoles, Long roleId) {
        boolean flag = false;
        try {
            repository.deleteByRoleId(roleId);
            repository.saveAll(resourceRoles);
            flag = true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }


    /**
     * @param roleId
     * @return java.util.List<java.lang.Long>
     * @depict 获取当前角色的所有资源id集合
     * @auth Barry
     * @createDate 2020/2/21
     * @updateAuthOrOther
     */
    public List<Long> getTheResourceCollectionByRoleId(Long roleId) {
        return repository.findResourceIdsByRoleId(roleId);
    }

    public void deleteByResourceId(Long id) {
        repository.deleteByResourceId(id);
    }

    public void deleteByRoleId(Long id) {
        repository.deleteByRoleId(id);
    }

}
